package cn.edu.scau.cmi.chenjia.clientFxml;

import java.net.URL;
import java.util.ResourceBundle;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.chenjia.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.chenjia.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.chenjia.hibernate.domain.Student;
import cn.edu.scau.cmi.chenjia.hibernate.domain.Teacher;
import cn.edu.scau.cmi.chenjia.spring.domain.student;
import cn.edu.scau.cmi.chenjia.spring.domain.teacher;
import cn.edu.scau.cmi.chenjia.hibernate.util.ScauCmiHibernateSessionFactoryUtil;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;

public class SpringHibernateDaoUIController implements Initializable {
	@FXML
	private TextArea t1;
	@FXML
	private TextArea t2;
	@FXML
	private TextField f1;
	@FXML
	private TextField f2;
	@FXML
	private ComboBox s1;
	@FXML
	private ComboBox s2;
	@FXML
	private Button b1;
	@FXML
	private Button b2;

	StudentDAO studentDAO;
	TeacherDAO teacherDAO;
	Session session;
	ApplicationContext ac1;
	teacher liangzaoqing;
	student zhangsan;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
		s1.setValue("学生");
		s1.setItems(FXCollections.observableArrayList("学生", "老师"));
		s2.setItems(FXCollections.observableArrayList("查找", "添加", "修改", "删除", "查看表", "选择或修改指导老师"));
		ac1=getApplicationContext1();
	}

	private static ApplicationContext getApplicationContext1() {
		ApplicationContext ac1 = new ClassPathXmlApplicationContext("spring.xml");
		return ac1;
	}
	
	public void setItems() {
		s2.setValue("");
		switch ((String) s1.getValue()) {
		case "学生":
			s2.setItems(FXCollections.observableArrayList("查找", "添加", "修改", "删除", "查看表", "选择或修改指导老师"));
			break;
		case "老师":
			s2.setItems(FXCollections.observableArrayList("查找", "添加", "修改", "删除", "查看表", "选择或修改被指导学生"));
			break;
		default:
			break;
		}
	}

	public void point() {
		String a = (String) s2.getValue();
		if (a == "查找")
			t1.appendText("请输入学号/工号!" + '\n');
		else if (a == "添加")
			t1.appendText("请输入姓名!添加学生可在学号框输入指导老师ID,可为空" + '\n');
		else if (a == "修改")
			t1.appendText("请输入学号/工号以及姓名!" + '\n');
		else if (a == "删除")
			t1.appendText("请输入学号/工号!" + '\n');
		else if (a == "查看表")
			t1.appendText("点击按钮确认!" + '\n');
		else if (a == "选择或修改指导老师")
			t1.appendText("请在学号框输入学号，在姓名框输入工号!" + '\n');
		else if (a == "选择或修改被指导学生")
			t1.appendText("请在学号框输入工号，在姓名框输入学号!" + '\n');
	}

	public void button() {
		String st1;
		String st2;
		if ((String) s1.getValue() == "学生") {
			st1 = f1.getText();
			st2 = f2.getText();
			if ((String) s2.getValue() == "查找" && st1.length() > 0) {
				StudentDAO studentDAO = ac1.getBean("studentDAO",StudentDAO.class);
				Student student = studentDAO.findById(Long.parseLong(st1));
				if (student != null)
					t1.appendText("ID:" + student.getId() + "  " + "名字:" + student.getName() + "  " + "指导老师:"
							+ student.getTeacher() + '\n');
				else
					t1.appendText("该ID不存在!" + '\n');
			}

			else if ((String) s2.getValue() == "查看表") {
				StudentDAO studentDAO =ac1.getBean("studentDAO",StudentDAO.class);
				List students = studentDAO.findAll();
				Iterator<?> studentIterator = students.iterator();
				while (studentIterator.hasNext()) {
					Student student = (Student) studentIterator.next();
					t1.appendText("ID:" + student.getId() + "  " + "名字:" + student.getName() + "  " + "指导老师:"
							+ student.getTeacher() + '\n');
				}
			}

			else if ((String) s2.getValue() == "添加") {
				if (st2.length() > 0) {
					StudentDAO studentDAO = ac1.getBean("studentDAO",StudentDAO.class);
					List student = studentDAO.findByName(st2);
					if (student.size() != 0)
						t1.appendText("该名字已存在!" + '\n');
					else {
						Student student1 = new Student();
						student1.setName(st2);
						int x = 0;
						if (st1.length() > 0) {
							Student student2 = studentDAO.findById(Long.parseLong(st1));
							if (student2 != null) {
								TeacherDAO teacherDAO = new TeacherDAO();
								Teacher teacher = teacherDAO.findById(Long.parseLong(st1));
								student1.setTeacher(teacher);
								x = 1;
							}
						}
						studentDAO.save(student1);
						if (x == 1)
							t1.appendText("添加学生" + st2 + "成功!指导老师id为" + st1 + '\n');
						else
							t1.appendText("添加学生" + st2 + "成功!" + '\n');
					}

				}
			}

			else if ((String) s2.getValue() == "修改") {
				if (st2.length() > 0 && st1.length() > 0) {
					StudentDAO studentDAO = ac1.getBean("studentDAO",StudentDAO.class);
					Student student = studentDAO.findById(Long.parseLong(st1));
					if (student != null) {
						List student1 = studentDAO.findByName(st2);
						if (student.getName().equals(st2))
							t1.appendText("修改ID为" + st1 + "的学生名字为" + st2 + '\n');
						else if (student1.size() > 0)
							t1.appendText("该名字已存在!" + '\n');
						else if (student1.size() == 0) {
							student.setName(st2);
							studentDAO.attachDirty(student);
							t1.appendText("修改ID为" + st1 + "的学生名字为" + st2 + '\n');
						}
					} else
						t1.appendText("该ID不存在!" + '\n');
				}
			}

			else if ((String) s2.getValue() == "删除") {
				if (st1.length() > 0) {
					StudentDAO studentDAO = ac1.getBean("studentDAO",StudentDAO.class);
					Student student = studentDAO.findById(Long.parseLong(st1));
					if (student != null) {
						studentDAO.delete(student);
						t1.appendText("删除ID为" + st1 + "的学生成功!");
					} else
						t1.appendText("该ID不存在!" + '\n');
				}
			}

			else if ((String) s2.getValue() == "选择或修改指导老师") {
				if (st1.length() > 0 && st2.length() > 0) {
					StudentDAO studentDAO = ac1.getBean("studentDAO",StudentDAO.class);
					Student student = studentDAO.findById(Long.parseLong(st1));
					TeacherDAO teacherDAO = new TeacherDAO();
					Teacher teacher = teacherDAO.findById(Long.parseLong(st2));
					if (student != null & teacher != null) {
						student.setTeacher(teacher);
						studentDAO.attachDirty(student);
						t1.appendText("修改ID为" + st1 + "的学生的指导老师的ID为" + st2 + '\n');
					} else if (student == null)
						t1.appendText("该ID的学生不存在!" + '\n');
					else if (teacher == null)
						t1.appendText("该ID的老师不存在!" + '\n');
				}
			}
		}

		else if ((String) s1.getValue() == "老师") {
			st1 = f1.getText();
			st2 = f2.getText();
			if ((String) s2.getValue() == "查找" && st1.length() > 0) {
				TeacherDAO teacherDAO = ac1.getBean("teacherDAO",TeacherDAO.class);
				Teacher teacher = teacherDAO.findById(Long.parseLong(st1));
				if (teacher != null)
					t1.appendText("ID:" + teacher.getId() + "  " + "名字:" + teacher.getName() + '\n');
				else
					t1.appendText("该ID不存在!" + '\n');
			}

			else if ((String) s2.getValue() == "查看表") {
				TeacherDAO teacherDAO = ac1.getBean("teacherDAO",TeacherDAO.class);
				List teachers = teacherDAO.findAll();
				Iterator<?> teacherIterator = teachers.iterator();
				while (teacherIterator.hasNext()) {
					Teacher teacher = (Teacher) teacherIterator.next();
					t1.appendText("ID:" + teacher.getId() + "  " + "名字:" + teacher.getName() + '\n');
				}
			}

			else if ((String) s2.getValue() == "添加") {
				if (st2.length() > 0) {
					TeacherDAO teacherDAO = ac1.getBean("teacherDAO",TeacherDAO.class);
					List teacher = teacherDAO.findByName(st2);
					if (teacher.size() != 0)
						t1.appendText("该名字已存在!" + '\n');
					else {
						Teacher teacher1 = new Teacher();
						teacher1.setName(st2);
						teacherDAO.save(teacher1);
						Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
						Transaction transaction = session.beginTransaction();
						transaction.commit();
						session.close();
						t1.appendText("添加老师" + st2 + "成功!" + '\n');
					}

				}
			}

			else if ((String) s2.getValue() == "修改") {
				if (st2.length() > 0 && st1.length() > 0) {
					TeacherDAO teacherDAO = ac1.getBean("teacherDAO",TeacherDAO.class);
					Teacher teacher = teacherDAO.findById(Long.parseLong(st1));
					if (teacher != null) {
						List teacher1 = teacherDAO.findByName(st2);
						if (teacher.getName().equals(st2))
							t1.appendText("修改ID为" + st1 + "的老师名字为" + st2 + '\n');
						else if (teacher1.size() > 0)
							t1.appendText("该名字已存在!" + '\n');
						else if (teacher1.size() == 0) {
							teacher.setName(st2);
							teacherDAO.attachDirty(teacher);
							t1.appendText("修改ID为" + st1 + "的老师名字为" + st2 + '\n');
						}
					} else
						t1.appendText("该ID不存在!" + '\n');
				}
			}

			else if ((String) s2.getValue() == "删除") {
				if (st1.length() > 0) {
					TeacherDAO teacherDAO =  ac1.getBean("teacherDAO",TeacherDAO.class);
					Teacher teacher = teacherDAO.findById(Long.parseLong(st1));
					if (teacher != null) {
						teacherDAO.delete(teacher);
						t1.appendText("删除ID为" + st1 + "的老师成功!" + '\n');
					} else
						t1.appendText("该ID不存在!" + '\n');
				}
			}

			else if ((String) s2.getValue() == "选择或修改被指导学生") {
				if (st1.length() > 0 && st2.length() > 0) {
					StudentDAO studentDAO = ac1.getBean("studentDAO",StudentDAO.class);
					Student student = studentDAO.findById(Long.parseLong(st2));
					TeacherDAO teacherDAO = ac1.getBean("teacherDAO",TeacherDAO.class);
					Teacher teacher = teacherDAO.findById(Long.parseLong(st1));
					if (student != null & teacher != null) {
						student.setTeacher(teacher);
						studentDAO.attachDirty(student);
						t1.appendText("修改ID为" + st1 + "的老师指导的学生的ID为" + st2 + '\n');
					} else if (student == null)
						t1.appendText("该ID的学生不存在!" + '\n');
					else if (teacher == null)
						t1.appendText("该ID的老师不存在!" + '\n');
				}
			}
		}
	}

	public void button2() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("springApplicationContext.xml");
		zhangsan=ac.getBean("zhangsan",student.class);
		t2.appendText(zhangsan.getName()+"的毕业设计指导导师是："+zhangsan.getTeacher().getName());
	}
}
